package com.my.ads.lib;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.NativeExpressAdView;
import com.google.android.gms.ads.VideoOptions;
import com.my.ads.lib.bean.AdList;
import com.my.ads.lib.bean.AdxBannerAd;
import com.my.ads.lib.bean.AdxNativeAd;
import com.my.ads.lib.utils.Utils;

import java.util.List;


/**
 * Created by xuanyang on 2018/3/16.
 */

public class AdxUtils {


    private static AdxUtils instance = new AdxUtils();

    public static AdxUtils getInstance() {
        return instance;
    }


    /////////////////
    public void cacheAdxInterstitialAd(Context context) {
        if (AdList.AdxInterList.size() < Config.InterSize) {
            requestInterstitialAd(context, Config.AdxInterKey, AdList.AdxInterList);
            if (AdList.AdxInterBackList.size() < Config.InterBackSize) {
                requestInterstitialAd(context, Config.AdxInterBackKey, AdList.AdxInterBackList);
            }
        }
    }

    private void requestInterstitialAd(final Context context, final String adxInterKey, final List<InterstitialAd> adxInterList) {

        Log.i(Config.TagLog, "adx i begin");

        final com.google.android.gms.ads.InterstitialAd mInterstitialAd = new com.google.android.gms.ads.InterstitialAd(context);
        mInterstitialAd.setAdUnitId(adxInterKey);
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdFailedToLoad(int i) {
                Log.i(Config.TagLog, "adx i error : " + i);
            }

            @Override
            public void onAdLoaded() {
                Log.i(Config.TagLog, "adx i success");
                addInterstitialAd(mInterstitialAd, adxInterList);
            }
        });
    }

    private void addInterstitialAd(InterstitialAd mInterstitialAd, List<InterstitialAd> adxInterList) {
        adxInterList.add(0, mInterstitialAd);
    }


    /////////////////
    public void cacheAdxNativeAd(Context context) {
        if (AdList.AdxNativeList.size() < Config.NativeSize) {
            Log.i(Config.TagLog, "adx begin");
            requestNativeAd(context, Config.AdxNativeKey, AdList.AdxNativeList);
            if (AdList.AdxNativeBackList.size() < Config.NativeBackSize) {
                requestNativeAd(context, Config.AdxNativeBackKey, AdList.AdxNativeBackList);
            }
        }
    }

    private void requestNativeAd(final Context context, final String adxNativeKey, final List<AdxNativeAd> adxNativeList) {
        final NativeExpressAdView adView = new NativeExpressAdView(context);
        loadNewNative(context, adView, adxNativeKey, new AdListener() {

            @Override
            public void onAdFailedToLoad(int i) {
                Log.i(Config.TagLog, "adx n error : " + i);
            }

            @Override
            public void onAdLoaded() {
                Log.i(Config.TagLog, "adx n success");
                addNativeAd(adView, adxNativeList);
            }
        });
    }

    /**
     * 加载新的原生，传入id和监听，其他统一属性
     */
    public static void loadNewNative(Context context, NativeExpressAdView adView, String nativeId,
                                     AdListener adListener) {
        adView.setAdUnitId(nativeId);
        adView.setAdListener(adListener);
        adView.setAdSize(Utils.getAdSize(context));
        adView.setVideoOptions(new VideoOptions.Builder()
                .setStartMuted(true)
                .build());
        adView.loadAd(new AdRequest.Builder().build());
    }

    private void addNativeAd(NativeExpressAdView adView, List<AdxNativeAd> adxNativeAdList) {

        try {
            AdxNativeAd adxNativeAd = new AdxNativeAd();
            adxNativeAd.setNativeAd(adView);
            adxNativeAd.setHasShowed(false);
            adxNativeAd.setAdTime(System.currentTimeMillis());
            adxNativeAdList.add(0, adxNativeAd);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //////////////////
    public void cacheAdxBannerAd(final Context context) {
        if (AdList.AdxBannerList.size() <= Config.BannerSize) {
            Log.i(Config.TagLog, "adx b begin");
            final AdView adView = new AdView(context);
            adView.setAdSize(AdSize.BANNER);
            adView.setAdUnitId(Config.AdxBannerKey);
            adView.setAdListener(new AdListener() {
                @Override
                public void onAdLoaded() {
                    Log.i(Config.TagLog, "adx b success");
                    AdxBannerAd localAdxBannerAd = new AdxBannerAd();
                    localAdxBannerAd.setNativeAd(adView);
                    localAdxBannerAd.setHasShowed(false);
                    localAdxBannerAd.setAdTime(System.currentTimeMillis());
                    AdList.AdxBannerList.add(0, localAdxBannerAd);
                    AdList.AdxBannerList = Utils.removeBannerAdByTimeout(AdList.AdxBannerList);
                }
            });
            adView.loadAd(new AdRequest.Builder().build());

            if (AdList.AdxBannerBackList.size() <= Config.BannerBackSize) {
                final AdView other = new AdView(context);
                other.setAdSize(AdSize.BANNER);
                other.setAdUnitId(Config.AdxBannerBackKey);
                other.setAdListener(new AdListener() {
                    @Override
                    public void onAdLoaded() {
                        AdxBannerAd localAdxBannerAd = new AdxBannerAd();
                        localAdxBannerAd.setNativeAd(other);
                        localAdxBannerAd.setHasShowed(false);
                        localAdxBannerAd.setAdTime(System.currentTimeMillis());
                        AdList.AdxBannerBackList.add(0, localAdxBannerAd);
                        AdList.AdxBannerBackList = Utils.removeBannerAdByTimeout(AdList.AdxBannerBackList);
                    }
                });
                other.loadAd(new AdRequest.Builder().build());
            }
        }
    }


    ////////////////////
    public void showAdxInterstitialAd(Context context, String targetActivity) {

        try {
            if (AdList.AdxInterList != null && AdList.AdxInterList.size() > 0) {
                InterstitialAd interstitialAd = AdList.AdxInterList.get(0);
                AdList.AdxInterList.remove(0);
                adxInterstitialAd(context, targetActivity, interstitialAd);
            } else if (AdList.AdxInterBackList != null && AdList.AdxInterBackList.size() > 0) {
                InterstitialAd interstitialAd = AdList.AdxInterBackList.get(0);
                AdList.AdxInterBackList.remove(0);
                adxInterstitialAd(context, targetActivity, interstitialAd);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void adxInterstitialAd(final Context context, final String targetActivity, InterstitialAd interstitialAd) {
        Log.i(Config.TagLog, "adx n show");
        interstitialAd.show();
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                super.onAdClosed();
                if (!TextUtils.isEmpty(targetActivity)) {
                    Utils.startTargetActivity(context, targetActivity);
                }
            }
        });
        SharedPreferences sp = context.getSharedPreferences(Config.InterstitialAdTarget, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();

        editor.putLong(Config.InterstitialAdTimeShow, System.currentTimeMillis());
        editor.apply();
    }


    /////////////////////////////
    public void showAdxNativeAd(Context context, RelativeLayout parent, boolean isFromLockScreen) {

        try {
            if (parent == null) {
                return;
            }
            if (parent.getChildCount() != 0) {
                parent.removeAllViews();
            }
//            if (isFromLockScreen) {
//                return;
//            }
            adxNativeAd(context, parent);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void adxNativeAd(Context context, final RelativeLayout parent) {
        Log.i(Config.TagLog, "adx n begin");
        try {
            if (AdList.AdxNativeList != null && AdList.AdxNativeList.size() > 0) {
                makeAdxNativeAd(context, parent, AdList.AdxNativeList);
            } else if (AdList.AdxNativeList != null && AdList.AdxNativeBackList.size() > 0) {
                makeAdxNativeAd(context, parent, AdList.AdxNativeBackList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void makeAdxNativeAd(final Context context, final RelativeLayout parent, List<AdxNativeAd> adxNativeAdList) {

        Log.i(Config.TagLog, "adx n show");
        parent.setVisibility(View.VISIBLE);
        NativeExpressAdView adView = adxNativeAdList.get(0).getNativeAd();
        ViewGroup viewGroup = (ViewGroup) adView.getParent();
        if (viewGroup != null) {
            viewGroup.removeAllViews();
        }
        Utils.addCenterInParentToAdView(adView);
        parent.addView(adView);
        adxNativeAdList.remove(0);
        Utils.recordNativeShowTime(context);
    }


    ///////////////////////
    public void showAdxBannerAd(final RelativeLayout parent) {

        try {
            if (parent == null) {
                return;
            }

            Log.i(Config.TagLog, "adx b show");

            parent.setVisibility(View.GONE);

            if (AdList.AdxBannerList != null && AdList.AdxBannerList.size() > 0) {
                parent.setVisibility(View.VISIBLE);
                ViewGroup viewGroup = (ViewGroup) AdList.AdxBannerList.get(0).getNativeAd().getParent();
                if (viewGroup != null) {
                    viewGroup.removeAllViews();
                }

                parent.removeAllViews();
                parent.setGravity(Gravity.CENTER);
                parent.addView(AdList.AdxBannerList.get(0).getNativeAd());

                AdList.AdxBannerList.get(0).setHasShowed(true);
                AdList.AdxBannerList.remove(0);

            } else if (AdList.AdxBannerBackList != null && AdList.AdxBannerBackList.size() > 0) {
                parent.setVisibility(View.VISIBLE);
                ViewGroup viewGroup = (ViewGroup) AdList.AdxBannerBackList.get(0).getNativeAd().getParent();
                if (viewGroup != null) {
                    viewGroup.removeAllViews();
                }

                parent.removeAllViews();
                parent.setGravity(Gravity.CENTER);
                parent.addView(AdList.AdxBannerBackList.get(0).getNativeAd());

                AdList.AdxBannerBackList.get(0).setHasShowed(true);
                AdList.AdxBannerBackList.remove(0);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}